package com.skt.common_hr.appmss.model
{
	import com.skt.common_hr.appmss.model.vo.MemberVO;
	import com.skt.common_hr.appmss.AppmssFacade;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import org.openzet.utils.DateUtil;
	
	public class OrgProxy extends CommonProxy
	{
		public static const NAME:String = "OrgProxy";
		
		public var orgList:XMLList;
		public var cdpList:ArrayCollection;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function OrgProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, auth:String):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			param["I_AUTH"] = auth;
			
			loadData("Z_HR_0015", param, result, fault); 
		}

//		public function cdpLoad(pernr:String):void
//		{
//			var param:Object = new Object();
//			param["I_PERNR"] = pernr;
//			param["I_MTYPE"] = 'C1';
//			
//			loadData("Z_HR_I001", param, resultCdp, fault);
//		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {  
				orgList = parseOrgList(event.result.RTAB);
				facade.sendNotification(AppmssFacade.LOAD_ORGLIST_COMPLETE, this);
			}
			else {
				trace( "failed to get post list");
				facade.sendNotification(AppmssFacade.LOAD_ORGLIST_FAILED, this);
			}
		}
		
//		public function resultCdp(event:ResultEvent):void
//		{
//			if(retCode==0) {   
//				cdpList = parseCdpList(event.result.OTAB1);
//				facade.sendNotification(AppmssFacade.LOAD_ORGLIST_COMPLETE, this);
//			}
//			else {
//				trace( "failed to get post list");
//				facade.sendNotification(AppmssFacade.LOAD_ORGLIST_FAILED, this);
//			}
//		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace(event.fault.faultString);
			facade.sendNotification(AppmssFacade.LOAD_ORGLIST_FAILED, this);
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 인사기록 Value Object
		 */
		private function parseOrgList(retObj:ArrayCollection):XMLList
		{
			var prevArr:Array = new Array();
			var root:XML;
			if(retObj.length>0) {
				root = <node label={retObj[0].STEXT} objid={retObj[0].OBJID} level={retObj[0].LEVEL} parent={retObj[0].UPPERORG}/>;
				prevArr[root.@level] = root;
							
				for(var i:int=1; i<retObj.length; i++)
				{
					var rec:Object = retObj[i];
					var node:XML = <node label={rec.STEXT} objid={rec.OBJID} level={rec.LEVEL} parent={rec.UPPERORG}/>;
					if(prevArr[parseInt(rec.LEVEL)-1]!=null) {
						XML(prevArr[parseInt(rec.LEVEL)-1]).appendChild(node);
					}
					prevArr[node.@level] = node;
				}
			}
			else {
				root = <node label="No Data" objid="0000" level="0" parent="0000"/>;
			}
			//trace(root.toXMLString());
			return new XMLList(root);
		}
		
//		private function parseCdpList(srcArr:ArrayCollection):ArrayCollection
//		{
//			var retArr:ArrayCollection = new ArrayCollection();
//			
//			for each(var rec:Object in srcArr) 
//			{
//				
//				var vo:MemberVO = new MemberVO();
//				vo.CDPYR		= rec["CDPYR"];
//				vo.ATRACK 		= rec["ATRACK"];
//				vo.ATRTXT		= rec["ATRTXT"];
//				vo.APOTXT 		= rec["APOTXT"];
//				vo.ABIGO		= rec["ABIGO"];
//				vo.PERNR		= rec["PERNR"];
//				vo.ENAME		= rec["ENAME"];
//				
//				retArr.addItem(vo);
//			}
//			return retArr;	
//		}
		
	}
}